<template>
  <div class="app-container">
    <el-form ref="queryForm" :model="queryParams" size="mini" inline style="white-space: nowrap; overflow-x: auto">
      <el-form-item prop="versionCode">
        <el-input v-model.trim="queryParams.versionCode" placeholder="请输入版本号" clearable @keyup.enter.native="handleQuery" style="width: 130px" />
      </el-form-item>
      <el-form-item prop="versionName">
        <el-input v-model.trim="queryParams.versionName" placeholder="请输入版本名称" clearable @keyup.enter.native="handleQuery" style="width: 130px" />
      </el-form-item>
      <el-form-item>
        <el-button size="mini" type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button size="mini" icon="el-icon-refresh" @click="resetQuery">重置</el-button>
        <el-button size="mini" type="primary" icon="el-icon-plus" @click="showEditItem = true">新增</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="listLoading" :data="list" size="small" height="calc(100vh - 150px)">
      <el-table-column label="序号" type="index" width="50" />
      <el-table-column label="版本号" show-overflow-tooltip min-width="100">
        <router-link slot-scope="{ row }" :to="`/checkScore/evalChkitemCfg/${row.versionCode}?versionId=${row.versionId}`" class="link-type">
          {{ row.versionCode }}
        </router-link>
      </el-table-column>
      <el-table-column label="版本名称" prop="versionName" show-overflow-tooltip min-width="100" />
      <el-table-column label="启用状态" align="center" width="70">
        <div slot-scope="{ row }" @click="toggleIsEnable(row)" style="display: inline-block; cursor: pointer">
          <i class="el-icon-loading" v-if="!!row.isEnableLoading" />
          <el-switch class="g-switch-in" v-model="row.isEnable" :active-value="1" :inactive-value="0" active-text="启用" inactive-text="禁用" :width="50" style="pointer-events: none" v-else />
        </div>
      </el-table-column>
      <el-table-column label="生效时间" align="center" min-width="100">
        <span slot-scope="{ row }">{{ row.startDate | dateFilter('yyyy-mm-dd') }}</span>
      </el-table-column>
      <el-table-column label="失效时间" align="center" min-width="100">
        <span slot-scope="{ row }">{{ row.endDate | dateFilter('yyyy-mm-dd') }}</span>
      </el-table-column>
      <el-table-column label="录入人" prop="creator" align="center" show-overflow-tooltip min-width="90" />
      <el-table-column label="录入时间" prop="createTime" align="center" width="150" />
      <el-table-column label="操作" fixed="right" align="center" width="180">
        <template slot-scope="{ row }">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="$refs.EditItem.open(row)">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" :loading="row.removeLoading" @click="handleDelete(row)">删除</el-button>
          <el-button size="mini" type="text" icon="el-icon-plus" @click="$refs.CopyItem.open(row)">复制版本</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" /> -->

    <EditItem ref="EditItem" v-model="showEditItem" @ok="getList" />

    <CopyItem ref="CopyItem" v-model="showCopyItem" @ok="getList" />
  </div>
</template>
<script>
// 工程质量-检查项版本
import { listVersion, updateVersion, delVersion } from '@/api/checkScore/cqEval'
import EditItem from './components/EditEvalVersion'
import CopyItem from './components/CopyEvalVersion'
export default {
  name: 'EvalVersion',
  components: { EditItem, CopyItem},
  data() {
    return {
      listLoading: false,
      total: 0,
      list: [],
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        versionCode: '',
        versionName: ''
      },
      showEditItem: false,
      showCopyItem: false
    }
  },
  created() {
    this.getList()
  },
  methods: {
    async getList() {
      this.listLoading = true
      const res = await listVersion(this.queryParams).catch(() => false)
      this.listLoading = false
      if (res === false) return

      const data = res.data || []
      data.forEach(item => {
          item.copyVersionId = item.versionId
        })
      data.map(i => {
        i.isEnableLoading = false
        i.removeLoading = false
      })
      this.list = data
      this.total = res.total || 0
    },
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    async toggleIsEnable(item) {
      const isEnable = item.isEnable ? 0 : 1
      const name = isEnable === 1 ? '启用' : '禁用'
      const res1 = await this.$confirm(`确认${name}?`, '警告', { type: 'warning' }).catch(() => false)
      if (res1 !== 'confirm') return

      const opt = { ...item, isEnable }
      item.isEnableLoading = true
      const res = await updateVersion(opt).catch(() => false)
      item.isEnableLoading = false
      if (res === false) return

      this.$message.success(`已${name}`)
      this.getList()
    },
    async handleDelete(item) {
      if (item.removeLoading) return
      const res1 = await this.$confirm(`确认删除：${item.versionCode} - ${item.versionName}?`, '警告', { type: 'warning' }).catch(() => false)
      if (res1 !== 'confirm') return

      item.removeLoading = true
      const res = await delVersion(item.versionId).catch(() => false)
      item.removeLoading = false
      if (res === false) return

      this.msgSuccess('删除成功')
      this.list.length > 1 ? this.getList() : this.handleQuery()
    }
  }
}
</script>
